package org.example.tlias.controller;


import org.example.tlias.pojo.Result;
import org.example.tlias.utils.AliOssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController

public class FileUploadController {
    @Autowired
    private AliOssUtil aliOssUtil;

/*    @PostMapping("/upload")
    public Result upload(String name, Integer age, MultipartFile file) throws IOException {

        String originalFilename = file.getOriginalFilename();
        String subfix = originalFilename.substring(originalFilename.lastIndexOf("."));
        String newFileName = UUID.randomUUID() + subfix;
        file.transferTo(new File("D:/aijava/image/"+newFileName));



        return Result.success();
    }*/
@PostMapping("/upload")
public Result upload(MultipartFile file)  {

    try {
        String originalFilename = file.getOriginalFilename();

        String extension = originalFilename.substring(originalFilename.lastIndexOf("."));

        String objectName = UUID.randomUUID().toString() + extension;

        String url = aliOssUtil.upload(file.getBytes(), objectName);

        return Result.success(url);
    } catch (IOException e) {
//        log.info("文件上传失败");
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return Result.error("文件上传失败");
}
}
